﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AppiSynchro.Library.Helpers;
using Microsoft.Win32;
using System.IO;

namespace AppiSynchro.Library.ApplicationInfo
{
    public class SemiRegistryAI : IApplicationInfo
    {
        #region IApplicationInfo Membres

        String m_name;
        String m_version;
        String m_installPath;
        String m_latestVersion;
        bool m_isInstalled = false;
        IInstallationPipeLine m_installStrategy;

        public string Version
        {
            get { return m_version; }
        }

        public string InstallPath
        {
            get { return m_installPath; }
        }

        public string Name
        {
            get { return m_name; }
        }

        public bool IsInstalled()
        {
            return m_isInstalled;
        }

        public string Language
        {
            get { throw new NotImplementedException(); }
        }

        public void Init(string name, string latestVersion, Dictionary<string, string> keysList, IInstallationPipeLine installationPipeline)
        {
            m_name = name;
            m_latestVersion = latestVersion;
            m_installStrategy = installationPipeline;
            try
            {
                RegistryKey installKey = RegistryHelper.GetKey(keysList["InstallRegistryKey"]);

                m_installPath = RegistryHelper.GetValue(installKey, keysList["InstallRegistryPath"], keysList["InstallRegistryValue"]);

                String exe = Path.Combine(m_installPath, keysList["InstallAdditionalPath"]);

                m_version = VersionHelper.GetFileVersion(exe);

                if (m_version != null)
                    m_isInstalled = true;
            }
            catch (Exception e)
            {
                m_isInstalled = false;
            }
        }

        public bool HasNewestVersion()
        {
            if (Version != m_latestVersion)
                return true;

            return false;
        }

        public bool Install(string version)
        {
            return PipeLineHelper.Install(m_installStrategy, version);
        }

        public bool Install()
        {
            return PipeLineHelper.Install(m_installStrategy, m_latestVersion);
        }

        #endregion
    }
}
